package com.egor.views;

import com.egor.security.SecurityService;
import com.egor.views.components.DepartmentComponent;
import com.egor.views.components.EmployeeComponent;
import com.egor.views.components.OrderComponent;
import com.vaadin.flow.component.button.Button;
import com.vaadin.flow.component.orderedlayout.HorizontalLayout;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.component.tabs.TabSheet;
import com.vaadin.flow.router.PageTitle;
import com.vaadin.flow.router.Route;
import com.vaadin.flow.spring.annotation.RouteScope;

import javax.annotation.security.PermitAll;

@PageTitle("Main page")
@Route(value = "")
@RouteScope
@PermitAll
public class MainView extends VerticalLayout {
    private final HorizontalLayout toolbar = new HorizontalLayout();
    private final TabSheet tabSheet = new TabSheet();
    private final EmployeeComponent employeeComponent;
    private final DepartmentComponent departmentComponent;
    private final SecurityService securityService;
    private final OrderComponent orderComponent;

    public MainView(EmployeeComponent employeeComponent,
                    DepartmentComponent departmentComponent,
                    OrderComponent orderComponent,
                    SecurityService securityService) {
        this.employeeComponent = employeeComponent;
        this.departmentComponent = departmentComponent;
        this.securityService = securityService;
        this.orderComponent = orderComponent;

        addClassName("main");
        setSizeFull();

        setTabs();
        setToolbar();

        add(toolbar, tabSheet);
    }

    private void setToolbar() {
        Button logout = new Button("Logout");

        logout.addClickListener(event -> securityService.logout());

        toolbar.setWidthFull();
        toolbar.setJustifyContentMode(JustifyContentMode.END);
        toolbar.add(logout);
    }

    private void setTabs() {
        tabSheet.setSizeFull();
        tabSheet.add("Employees", employeeComponent);
        tabSheet.add("Department", departmentComponent);
        tabSheet.add("Orders", orderComponent);
    }
}
